using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.Managers;

namespace iPhOwa.Web.Controls
{
	public partial class CalendarContent : iPhOwaControl
	{
		/// <summary>
		/// Page Load event.
		/// </summary>
		protected void Page_Load(object sender, EventArgs e)
		{
			Folder currentFolder = FolderManager.Get(Path, OwaSession);

			if(currentFolder != null)
			{
				ICollection<Appointment> appointments = AppointmentManager.GetList(Path, OwaSession);

				IDictionary<DateTime, ICollection<Appointment>> dates = new Dictionary<DateTime, ICollection<Appointment>>();

				foreach(Appointment appointment in appointments)
				{
					if(appointment.AllDayEvent && appointment.DateEnd.Subtract(appointment.DateStart).TotalDays > 1)
					{
						DateTime day = appointment.DateStart.Date;

						while(day <= appointment.DateEnd.Date)
						{
							if(day >= DateTime.Now.Date)
							{
								if(!dates.ContainsKey(day))
								{
									dates.Add(day, new Collection<Appointment>());
								}

								dates[day].Add(appointment);
							}

							day = day.AddDays(1);
						}
					}
					else
					{
						if(!dates.ContainsKey(appointment.DateStart.Date))
						{
							dates.Add(appointment.DateStart.Date, new Collection<Appointment>());
						}

						dates[appointment.DateStart.Date].Add(appointment);
					}
				}

				if(dates.Count > 0)
				{
					dateList.DataSource = dates;
					dateList.DataBind();
				}
				else
				{
					dateList.Visible = false;
					noMessagesPanel.Visible = true;
				}
			}
		}
	}
}